﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi
{
    public partial class mfLogin : msBsc.Cls.mbPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strRetPath = HttpContext.Current.Request.Url.ToString();

                string strOl = HttpContext.Current.Request.QueryString.Get("OL");
                int item111 = strRetPath.ToLower().IndexOf("returnurl=");
                if (item111 != -1)
                {
                    strRetPath = strRetPath.Substring(item111 + 10, strRetPath.Length - item111 - 10);
                    if (HttpContext.Current.Request.ApplicationPath != "/")
                        strRetPath = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase.Substring(0, msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase.ToLower().LastIndexOf(HttpContext.Current.Request.ApplicationPath.ToLower())) + strRetPath;
                    //this.ExecJScript("window.location.href='" + msBsc.AppLogic.Cfg.HttpTools.EzUrlEncode(strRetPath) + "';");

                    this.ExecJScript("$('#txtRetUrl').val('" + strRetPath + "');");

                    //msBsc.AppLogic.Cfg.UserInfo oUserInfo = new AppLogic.Cfg.UserInfo();
                    if (msBsc.SvRef.UserInfo.Online)
                    {
                        if (!string.IsNullOrEmpty(strOl) && strOl == "1")
                        {
                            this.ExecJScript("$('#divtipinfo').show();");
                        }
                        else
                        {
                            this.Response.Redirect(strRetPath);
                        }
                    }
                }
                else if (this.Request.UrlReferrer != null)
                {
                    if (this.Request.UrlReferrer.PathAndQuery.ToLower().IndexOf("reg") != -1 || this.Request.UrlReferrer.PathAndQuery.ToLower().IndexOf("err") != -1)
                    {
                        this.ExecJScript("$('#txtRetUrl').val('" + "/c_CustManager.htm" + "');");
                    }
                    else
                    {
                        this.ExecJScript("$('#txtRetUrl').val('" + this.Request.UrlReferrer.PathAndQuery + "');");
                    }
                    //msBsc.AppLogic.Cfg.UserInfo oUserInfo = new AppLogic.Cfg.UserInfo();
                    if (msBsc.SvRef.UserInfo.Online)
                    {
                        if (!string.IsNullOrEmpty(strOl) && strOl == "1")
                        {
                            this.ExecJScript("$('#divtipinfo').show();");
                        }
                        else
                        {
                            this.Response.Redirect(this.Request.UrlReferrer.PathAndQuery);
                        }
                    }
                }
                else
                {
                    //msBsc.AppLogic.Cfg.UserInfo oUserInfo = new AppLogic.Cfg.UserInfo();
                    if (msBsc.SvRef.UserInfo.Online)
                    {
                        if (!string.IsNullOrEmpty(strOl) && strOl == "1")
                        {
                            this.ExecJScript("$('#divtipinfo').show();");
                        }
                        else
                        {
                            this.Response.Redirect("/mp_CustManager.htm");
                        }
                    }
                }
            }
        }
        public override void DoAction()
        {
            if (this.strAction == "REGIST_STEP1")
            {
                if (this.GetParamHtValue("USER_NME") == string.Empty || this.GetParamHtValue("USER_PASS") == string.Empty )
                {
                    Alert("登录不成功：请输入登录名、密码 ！");
                    return;
                } 
                //msBsc.AppLogic.Cfg.UserInfo oUserInfo = new AppLogic.Cfg.UserInfo();
                string strRet = msBsc.SvRef.UserInfo.Login(this.GetParamHtValue("USER_NME"), this.GetParamHtValue("USER_PASS"));
                if (strRet != "OK")
                {
                    strRet = msBsc.SvRef.UserInfo.LoginMail(this.GetParamHtValue("USER_NME"), this.GetParamHtValue("USER_PASS"));
                }
                if (strRet != "OK")
                {
                    Alert("输入的登录名或密码错误！");
                    return;
                }

                ////暂时的，以后不用
                //using (msBsc.AppLogic.DataAlc.CBizSysUser objuser = new AppLogic.DataAlc.CBizSysUser())
                //{
                //    objuser.AddCustomerInfo(this.GetParamHtValue("USER_NME"));
                //}


                this.ExecJScript("LS.set('CURR_LOGIN_USR_NME','" + this.GetParamHtValue("USER_NME") + "');");
                 
                this.ExecJScript("RetToUrl();");
                 
            }
        }

    }
}